/*

URL编解码
*/
function encodeData(){
	let val = document.getElementById("data").value;
	if(val){
		document.getElementById("appbox").value = encodeURIComponent(val.trim());
	}

}

function decodeData(){
	let val = document.getElementById("data").value;
	if(val){
		document.getElementById("appbox").value = decodeURIComponent(val.trim());
	}
}

function closeData(){
	document.getElementById("data").value = "";
	document.getElementById("appbox").value = "";
}


/**

排课工具
*/

const dayArray = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const dayChoiceArray = [];

function selectArray(arr, str) {
	let tmp = -1;
	for (var i = 0; i < dayChoiceArray.length; i++) {
		if (dayChoiceArray[i] == str) {
			tmp = i;
		}
	}
	return tmp;
}

function choiceDateBar() {
	let bar = this.innerHTML;
	let day = this.parentNode.parentNode.firstChild.innerHTML;
	day = day>9?day:"0"+day;
	let month = document.getElementById("course-month").value;
	let tmp = -1;
	for (var i = 0; i < dayChoiceArray.length; i++) {
		if (dayChoiceArray[i] == month + '-' + day + "-" + bar) {
			tmp = i;
		}
	}
	if (selectArray(dayChoiceArray, month + '-' + day + "-" + bar) >= 0) {
		//移除
		dayChoiceArray.splice(tmp, 1);
		//
		this.style.backgroundColor = '';
	} else {
		//添加
		dayChoiceArray.push(month + '-' + day + "-" + bar);
		this.style.backgroundColor = 'rgba(35, 217, 110, .8)';
	}

	document.getElementById("totalAll").innerHTML = dayChoiceArray.length;
}

function dateBarAll() {
	let day = this.parentNode.parentNode.firstChild.innerHTML;
	day = day>9?day:"0"+day;
	let month = document.getElementById("course-month").value;

	let bar1 = this.parentNode.firstChild.innerHTML;
	let bar2 = this.parentNode.firstChild.nextElementSibling.innerHTML;
	let bar3 = this.parentNode.firstChild.nextElementSibling.nextElementSibling.innerHTML;
	let bar4 = this.parentNode.firstChild.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML;
	if (selectArray(dayChoiceArray, month + '-' + day + "-" + bar1) == -1) {
		dayChoiceArray.push(month + '-' + day + "-" + bar1);
		this.parentNode.firstChild.style.backgroundColor = 'rgba(35, 217, 110, .8)';
	}
	if (selectArray(dayChoiceArray, month + '-' + day + "-" + bar2) == -1) {
		dayChoiceArray.push(month + '-' + day + "-" + bar2);
		this.parentNode.firstChild.nextElementSibling.style.backgroundColor = 'rgba(35, 217, 110, .8)';
	}
	if (selectArray(dayChoiceArray, month + '-' + day + "-" + bar3) == -1) {
		dayChoiceArray.push(month + '-' + day + "-" + bar3);
		this.parentNode.firstChild.nextElementSibling.nextElementSibling.style.backgroundColor =
			'rgba(35, 217, 110, .8)';
	}
	if (selectArray(dayChoiceArray, month + '-' + day + "-" + bar4) == -1) {
		dayChoiceArray.push(month + '-' + day + "-" + bar4);
		this.parentNode.firstChild.nextElementSibling.nextElementSibling.nextElementSibling.style.backgroundColor =
			'rgba(35, 217, 110, .8)';
	}
	document.getElementById("totalAll").innerHTML = dayChoiceArray.length;
}

function initCourse(e) {
	var val = e.target.value;
	if (val >= 0) {
		var courseDateEl = document.getElementById("course-date-box");
		courseDateEl.innerHTML = '';
		var day = dayArray[val];

		var date = new Date();

		var week = new Date(date.getFullYear(), val, 1);
		let weekDay = week.getDay();
		let tmpIndex = 0;
		if (weekDay == 0) {
			tmpIndex = 6;
		} else {
			tmpIndex = weekDay - 1;
		}
		for (let i = 0; i < tmpIndex; i++) {
			var divEl = document.createElement("div");
			divEl.className = "date-fill";
			courseDateEl.appendChild(divEl);
		}

		for (var i = 0; i < day; i++) {
			var divEl = document.createElement("div");
			var divBgEl = document.createElement("div");
			divBgEl.className = "date-bg";
			divBgEl.innerHTML = i + 1;
			divEl.appendChild(divBgEl);
			//上
			var divAMEl = document.createElement("div");
			divAMEl.className = "date-am";

			let divAM1El = document.createElement("div");
			divAM1El.innerHTML = "1";
			divAM1El.addEventListener('click', choiceDateBar, false);
			let divAM2El = document.createElement("div");
			divAM2El.innerHTML = "2";
			divAM2El.addEventListener('click', choiceDateBar, false);
			let divAM3El = document.createElement("div");
			divAM3El.innerHTML = "3";
			divAM3El.addEventListener('click', choiceDateBar, false);
			let divAM4El = document.createElement("div");
			divAM4El.innerHTML = "4";
			divAM4El.addEventListener('click', choiceDateBar, false);

			let divAM5El = document.createElement("div");
			divAM5El.innerHTML = "全选";
			divAM5El.addEventListener('click', dateBarAll, false);
			divAMEl.appendChild(divAM1El);
			divAMEl.appendChild(divAM2El);
			divAMEl.appendChild(divAM3El);
			divAMEl.appendChild(divAM4El);
			divAMEl.appendChild(divAM5El);

			//下
			var divPMEl = document.createElement("div");
			divPMEl.className = "date-pm";

			let divPM1El = document.createElement("div");
			divPM1El.innerHTML = "5";
			divPM1El.addEventListener('click', choiceDateBar, false);
			let divPM2El = document.createElement("div");
			divPM2El.innerHTML = "6";
			divPM2El.addEventListener('click', choiceDateBar, false);
			let divPM3El = document.createElement("div");
			divPM3El.innerHTML = "7";
			divPM3El.addEventListener('click', choiceDateBar, false);
			let divPM4El = document.createElement("div");
			divPM4El.innerHTML = "8";
			divPM4El.addEventListener('click', choiceDateBar, false);

			let divPM5El = document.createElement("div");
			divPM5El.innerHTML = "全选";
			divPM5El.addEventListener('click', dateBarAll, false);
			divPMEl.appendChild(divPM1El);
			divPMEl.appendChild(divPM2El);
			divPMEl.appendChild(divPM3El);
			divPMEl.appendChild(divPM4El);
			divPMEl.appendChild(divPM5El);

			//晚
			var divEVEl = document.createElement("div");
			divEVEl.className = "date-ev";

			let divEV1El = document.createElement("div");
			divEV1El.innerHTML = "9";
			divEV1El.addEventListener('click', choiceDateBar, false);
			let divEV2El = document.createElement("div");
			divEV2El.innerHTML = "10";
			divEV2El.addEventListener('click', choiceDateBar, false);
			divEVEl.appendChild(divEV1El);
			divEVEl.appendChild(divEV2El);

			divEl.appendChild(divAMEl);
			divEl.appendChild(divPMEl);
			divEl.appendChild(divEVEl);
			divEl.className = "date" + i;
			courseDateEl.appendChild(divEl);

		}
	}

}

//创建课表
function createCourse() {
	let month = document.getElementById("course-month").value;
	if (month < 0) {
		alert("请选择月份");
		return;
	}
	let data = document.getElementById("data").value;
	if (dayChoiceArray.length <= 0) {
		alert("请选择排课时间");
		return;
	}
	if (!data) {
		alert("请输入课程");
		return;
	}
	createMonthCourse(month,data);

}

//创建月课表
function createMonthCourse(month, data) {
	let arr = [];
	arr.push(['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'])
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	arr.push(['', '', '', '', '', '', '']);
	let dataList = data.split("\n");
	let arrSort = dayChoiceArray.sort();
	console.log(dataList,arrSort);
	for (var i = 0; i < arrSort.length; i++) {
		
		let tmpArr = arrSort[i].split("-");
		let month = tmpArr[0];
		let day = tmpArr[1];
		let bar = tmpArr[2];
		let year = new Date().getFullYear();
		
		let weekDay = new Date(year,parseInt(month),parseInt(day)).getDay();
		console.log(weekDay);
		let tmp = 0;
		if(weekDay == 0){
			tmp = 6;
		}else{
			tmp = weekDay-1;
		}
		arr[bar][tmp] += dataList[parseInt(i/4)]+"("+(parseInt(month)+1)+"."+day+")"+"\n"; 	
	}	
	JSONToExcelConvertor((parseInt(month)+1)+"月份课表",arr);
}
//创建周课表


function JSONToExcelConvertor(fileName, jsonData) {
	var json = jsonData;
	var excel = '<table>';
	for (let i = 0; i < json.length; i++) {
		const item = json[i];
		// 每次循环把每对儿键值的内容放在同一行上
		excel += "<tr>";
		for (let j = 0; j < item.length; j++) {
			const ele = item[j];
			excel += "<td>" + ele + "</td>";
		}
		excel += "</tr>"; // 一行结束
	}
	//table结束
	excel += "</table>";

	var excelFile =
		"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
	excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
	excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
	excelFile += '; charset=UTF-8">';
	excelFile += "<head>";
	excelFile += "<!--[if gte mso 9]>";
	excelFile += "<xml>";
	excelFile += "<x:ExcelWorkbook>";
	excelFile += "<x:ExcelWorksheets>";
	excelFile += "<x:ExcelWorksheet>";
	excelFile += "<x:Name>";
	excelFile += "{worksheet}";
	excelFile += "</x:Name>";
	excelFile += "<x:WorksheetOptions>";
	excelFile += "<x:DisplayGridlines/>";
	excelFile += "</x:WorksheetOptions>";
	excelFile += "</x:ExcelWorksheet>";
	excelFile += "</x:ExcelWorksheets>";
	excelFile += "</x:ExcelWorkbook>";
	excelFile += "</xml>";
	excelFile += "<![endif]-->";
	excelFile += "</head>";
	excelFile += "<body>";
	excelFile += excel; //将table 拼接
	excelFile += "</body>";
	excelFile += "</html>";

	var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
	// 给我们上面格式出来的内容设置一个URI(统一资源标志符)
	var link = document.createElement("a"); //创建a标签
	link.href = uri; // 把上文解析出来的链接赋值给上面创建的a标签
	link.style = "visibility:hidden"; //设置为不可见，当然设置也可以，看个人喜好
	link.download = fileName + ".xls"; //指定文件名和文件后缀格式
	// 不可以改为 xlsx格式，因为我们这个格式化是按照格式写出来的，而真正的excel文件格式很麻烦
	// 如果打不开，查看这个文件属性是否被锁定了，解除锁定即可。
	console.log(document.body);
	document.body.appendChild(link); //追加a标签到html内部
	// 当然不放也能点击，但是为了保险起见，还是放一下
	link.click(); // 点击我们刚刚创建的a标签
	document.body.removeChild(link); // 把刚刚创建的a标签删除了
}


//字符串工具
function toStringUtil(){
	let textData = document.getElementById("text-data");
	let value = textData.value.trim();

	
	document.getElementById("length").innerText = value.length;
	document.getElementById("not-space").innerText = value.replace(/\s+/g, '');
	document.getElementById("not-space-length").innerText = value.replace(/\s+/g, '').length
}




